"""
自定义认证后端，实现多账号登陆
"""

from django.contrib.auth.backends import ModelBackend
from django.db.models import Q
from users.models import User

class UsernameMobileAuthBackend(ModelBackend):

    # 重写该方法，实现多账号验证
    def authenticate(self, request, username=None, password=None, **kwargs):
        """
        功能：根据身份信息验证用户身份
        参数：request请求对象；username前端传入的用户名或手机号等；password密码；
        返回值：返回用户对象或None
        """
        try:
            # 1、根据username、mobile去查找用户
            # select * from tb_users where username='18588269037' or mobile='18588269037';
            user = User.objects.get(
                Q(username=username) | Q(mobile=username) # username='18588269037' or mobile='18588269037'
            )
            # user = User.objects.get(username=username, mobile=username) # username=18588269037 and mobile=18588269037
        except User.DoesNotExist as e:
            return None

        # 2、检查密码
        if user.check_password(password):
            return user